import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class mainClass {
	
	public static String site;
	public static List<String> fileList = new ArrayList();
	
	public static void fetchImages(String path, String fileName) throws IOException {
		File file = new File(fileName);
		Document doc = Jsoup.parse(file, "UTF-8", path);
		int imageId = 0;
		String dir = "C:/DownloadedTempFiles/img/";
		Elements images = doc.select("img");


		for (Element el : images) {
			String url = el.absUrl("src");
			System.out.println("Image Found!");
			System.out.println("src attribute is : " + url);
			String extension = url.substring(url.lastIndexOf("."));
			System.out.println(extension);
			if (extension.equals(".png")||extension.equals(".jpg")||extension.equals(".gif")) {
				imageId +=1;
				String filename = dir + "img" + imageId + extension;
				saveImage(url, filename);
			}
		}
	}

	public static void saveImage(String imageUrl, String destinationFile) throws IOException {
		File dir = new File("C:/DownloadedTempFiles/img/");
		dir.mkdir();
		URL url = new URL(imageUrl);
		InputStream is = url.openStream();
		OutputStream os = new FileOutputStream(destinationFile);
		byte[] b = new byte[2048];
		int length;

		try {
			while ((length = is.read(b)) != -1) {
				os.write(b, 0, length);
			}
		} catch (MalformedURLException mue) {
		     mue.printStackTrace();
		} catch (IOException ioe) {
		     ioe.printStackTrace();
		} finally {
		    try {
		        if (is != null) is.close();
		        if (os != null) os.close();

		    } catch (IOException ioe) {}
		}
	}

	public static String downloadURL (String urlString, String outputFolder) throws IOException{
		
		URL u = new URL(urlString);
		String urlPath = u.getPath();
		String lastPathPart = FilenameUtils.getName(urlPath);
		String fileName=null;
		
		if (urlPath.isEmpty() || !lastPathPart.contains(".")){
			fileName = "index.html";
		}
		else{
			fileName = lastPathPart;
		}
		String filePathAndFileName = FilenameUtils.concat(outputFolder,fileName);
		System.out.println(filePathAndFileName);
		File f = new File( filePathAndFileName);
		FileUtils.copyURLToFile(u, f);
		return filePathAndFileName;
	}
	
	public static void generateFileList(File node){
	 
		if(node.isFile()){
			String str = node.getAbsoluteFile().toString();
			fileList.add(str.substring("C:/".length(), str.length()));
		}
	 
		if(node.isDirectory()){
			String[] subNote = node.list();
			for(String filename : subNote){
				generateFileList(new File(node, filename));
			}
		}
    }
	
	public static void zipDownloadedFiles() throws IOException{
		URL u = new URL(site);
		String urlPath = u.getPath();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		Date date = new Date();

		String outputZipFileName = "C:/ArchiveRepository/" + "Archive" + urlPath + dateFormat.format(date) +".zip";
		String sourceFolder = "C:/DownloadedTempFiles";
		byte[] buffer = new byte[1024];
		
		FileOutputStream fos = new FileOutputStream(outputZipFileName);
    	ZipOutputStream zos = new ZipOutputStream(fos);
		
    	generateFileList(new File(sourceFolder));
    	
		for(String file : fileList){
			 
    		System.out.println("File Added : " + file);
    		ZipEntry ze= new ZipEntry(file);
        	zos.putNextEntry(ze);
 
        	FileInputStream in = 
                       new FileInputStream(File.separator + file);
        	//new FileInputStream(sourceFolder + File.separator + file);
 
        	int len;
        	while ((len = in.read(buffer)) > 0) {
        		zos.write(buffer, 0, len);
        	}
 
        	in.close();
    	}
 
    	zos.closeEntry();
    	zos.close();
    	fileList = new ArrayList();
    	
    	FileUtils.deleteQuietly(new File("C:/DownloadedTempFiles"));
    			
		System.out.println();
		System.out.println("Ready!");
		System.out.println();
	}
	
	public static void ProcessInput() throws IOException{
		Scanner scanner = new Scanner(System.in);
		
		while(true){
			System.out.println("Welcome!");
			System.out.println("Please enter a web adress! For exit type: EXIT.");
			System.out.println("Your web address is: ");
			String userInput = scanner.nextLine();
			
			if (userInput.equalsIgnoreCase("EXIT")) {
				break;
			}
			
			site = userInput;
			String fileName = downloadURL(site, "C:/DownloadedTempFiles");
			fetchImages(site, fileName);
			zipDownloadedFiles();
		}
	}
	
	public static void main(String[] args) throws IOException{
		ProcessInput();
	}
}